function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return r[2];
    return '';
}
function addCookie(name, value, expireHours) {
    var cookieString = name + "=" + escape(value) + "; path=/";
    //判断是否设置过期时间
    if (expireHours > 0) {
        var date = new Date();
        date.setTime(date.getTime() + expireHours * 3600 * 1000);
        cookieString = cookieString + ";expires=" + date.toGMTString();
    }
    document.cookie = cookieString;
}

function getCookie(name) {
    var strcookie = document.cookie;
    var arrcookie = strcookie.split("; ");
    for (var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == name)return unescape(arr[1]);
    }
    return null;
}

function delCookie(name) {//删除cookie
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + "; path=/;expires=" + exp.toGMTString();
}
/*
 *ajax   post提交数据 
 */
function ajax_post(url, result_url, query) {
    $.post(url, query).success(function (data) {
        if (data.code == "200") {
            $(".alert").text('操作成功，页面即将自动跳转~');
            $(".alert").addClass('block').fadeIn(200);
            $(".alert").removeClass('alert-danger alert-warning alert-info alert-success').addClass('alert-success');
            setTimeout(function () {
                if (!result_url) {
                    location.reload();
                } else {
                    location.href = result_url;
                }
            }, 3000);
        } else {
            $(".alert").text(data.message);
            $(".alert").addClass('block').fadeIn(200);
            $(".alert").removeClass('alert-danger alert-warning alert-info alert-success').addClass('alert-danger');
            setTimeout(function () {
                $(".alert").removeClass('block');
                $("#alert").css('display', 'none');
            }, 3000);
        }
    });
}
/*
 * 分页
 */
//点击跳转页面
function pageup(pageNum, pageCount, pageCounts) {
    switch (pageNum) {
        case 1:
            page_icon(1, 5, 0, pageCounts);
            break;
        case 2:
            page_icon(1, 5, 1, pageCounts);
            break;
        case pageCount - 1:
            page_icon(pageCount - 4, pageCount, 3, pageCounts);
            break;
        case pageCount:
            page_icon(pageCount - 4, pageCount, 4, pageCounts);
            break;
        default:
            page_icon(pageNum - 2, pageNum + 2, 2, pageCounts);
            break;
    }
}
//根据当前选中页生成页面点击按钮
function page_icon(page, count, eq, pageCounts) {
    var ul_html = '<li class="paginate_button previous /*disabled*/ pageUpss" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_previous">'
        + '<a onclick="pageUpss(' + pageCounts + ');">上一页</a>'
        + '</li>';
    if (pageCounts <= 5) {
        count = pageCounts;
    }

    for (var i = page; i <= count; i++) {
        if (i == 0) {
            continue;
        }
        ul_html += '<li class="paginate_button" aria-controls="DataTables_Table_0" tabindex="0"><a onclick="page(' + i + "," + pageCounts + ');">' + i + '</a></li>';
    }
    ul_html += '<li class="paginate_button previous pageDowns" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_previous">'
        + '<a onclick="pageDowns(' + pageCounts + ');">下一页</a>'
        + '</li>';

    $("#page ul").html(ul_html);
    if (page == 0) {
        $("#page ul li").eq(eq).addClass("active");
    } else {
        $("#page ul li").eq(eq + 1).addClass("active");
    }
}
function pageUpss(pageCount) {
    var pageNum = parseInt($("#page li.active a").html());//获取当前页
    if (pageNum == 1) {
        $("#page .pageUpss").addClass("disabled");
    } else {
        list_value(pageNum - 1);
        pageup(pageNum - 1, pageCount, pageCount);
    }

}
function pageDowns(pageCount) {
    var pageNum = parseInt($("#page li.active a").html());//获取当前页
    if (pageNum == pageCount) {
        $("#page .pageDowns").addClass("disabled");
    } else {
        list_value(pageNum + 1);
        pageup(pageNum + 1, pageCount, pageCount);
    }

}
function page(page, pageCount) {
    list_value(page);
    pageup(page, pageCount, pageCount);
}
//上一页
function pageUp(pageNum, pageCount) {
    switch (pageNum) {
        case 1:
            break;
        case 2:
            page_icon(1, 5, 0);
            break;
        case pageCount - 1:
            page_icon(pageCount - 4, pageCount, 2);
            break;
        case pageCount:
            page_icon(pageCount - 4, pageCount, 3);
            break;
        default:
            page_icon(pageNum - 2, pageNum + 2, 1);
            break;
    }
}
//下一页
function pageDown(pageNum, pageCount) {
    switch (pageNum) {
        case 1:
            page_icon(1, 5, 1);
            break;
        case 2:
            page_icon(1, 5, 2);
            break;
        case pageCount - 1:
            page_icon(pageCount - 4, pageCount, 4);
            break;
        case pageCount:
            break;
        default:
            page_icon(pageNum - 2, pageNum + 2, 3);
            break;
    }
}
/*分页end*/
var token = getCookie("token");
$.ajaxSetup({
    global: true,
    dataType: 'json',
    headers: {
        'Token': token
    }
});